package com.huineta.business.modules.sys.controller;

import com.huineta.business.modules.sys.domain.SysMenu;
import com.huineta.business.modules.sys.service.ISysMenuService;
import com.huineta.business.common.aop.annotation.Log;
import com.huineta.business.common.constant.ControllerConstant;
import com.huineta.business.common.constant.TenantConstant;
import com.huineta.business.common.web.base.BaseController;
import com.huineta.business.common.web.domain.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Author:       Dmm Lee< bclassline@gmail.com >
 * Date:         2022/11/28
 * Description:  权限控制器
 */
@Api(tags = {"权限"})
@RestController
@RequestMapping(ControllerConstant.PREFIX_SYS + "menu")
public class SysMenuController extends BaseController {

    @Resource
    private ISysMenuService sysMenuService;

    /**
     * 查询权限
     */
    @GetMapping("tree")
    @Log(title = "查询权限")
    @ApiOperation(value = "查询权限")
    public Result tree(){
        Map map = new HashMap();
        map.put(null,null);

        return success(sysMenuService.tree());
    }

    /**
     * 可分配的权限列表
     *
     * @return sysPower 权限实体
     * */
    @GetMapping("tree/assign")
    @ApiOperation(value = "可分配的权限")
    public Result treeAssign(){
        // Tenant 模式下 tenant_power 关联查询
        if(TenantConstant.enable) {
            return success(sysMenuService.treeByTenantId());
        } else {
            return success(sysMenuService.tree());
        }
    }

    /**
     * 新增权限
     *
     * @param sysMenu 权限实体
     *
     * @return {@link Boolean}
     */
    @PostMapping("save")
    @Log(title = "新增权限")
    @ApiOperation(value = "新增权限")
    public Result save(@RequestBody SysMenu sysMenu){
        return auto(sysMenuService.save(sysMenu));
    }

    /**
     * 修改权限
     *
     * @param sysMenu 权限实体
     *
     * @return {@link Boolean}
     */
    @PutMapping("edit")
    @Log(title = "修改权限")
    @ApiOperation(value = "修改权限")
    public Result edit(@RequestBody SysMenu sysMenu){
        return auto(sysMenuService.updateById(sysMenu));
    }

    /**
     * 删除权限
     *
     * @param id 权限编号
     *
     * @return {@link Boolean}
     */
    @DeleteMapping("remove")
    @Log(title = "删除权限")
    @ApiOperation(value = "删除权限")
    public Result remove(String id){
        return auto(sysMenuService.removeById(id));
    }

}
